// 实现这个项目的构建任务

const sass = require("sass");
const loadGruntTasks = require("load-grunt-tasks");

const data = {
  menus: [
    {
      name: "Home",
      icon: "aperture",
      link: "index.html",
    },
    {
      name: "Features",
      link: "features.html",
    },
    {
      name: "About",

      link: "about.html",
    },
    {
      name: "Contact",

      link: "#",

      children: [
        {
          name: "Twitter",

          link: "https://twitter.com/w_zce",
        },
        {
          name: "About",

          link: "https://weibo.com/zceme",
        },
        {
          name: "divider",
        },
        {
          name: "About",

          link: "https://github.com/zce",
        },
      ],
    },
  ],
  pkg: require("./package.json"),

  date: new Date(),
};
module.exports = (grunt) => {
  grunt.initConfig({
    clean: {
      temp: ["temp", "dist"],
    },
    //转换html
    web_swig: {
      //任务名称就是下载插件的去掉grunt，之后的剩余字符串
      options: {
        swigOptions: {
          cache: false,
        },
        getData: function (tpl) {
          return data;
        },
      },
      dist: {
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["*.html"],
            dest: "temp",
            ext: ".html",
          },
        ],
      },
    },
    //转换scss
    sass: {
      options: {
        implementation: sass,
        sourceMap: true,
      },

      dist: {
        files: [
          {
            expand: true, //将启用下面的选项
            cwd: "src", //源文件路径
            src: ["**/*.scss"], //cwd的匹配模式
            dest: "temp", //目标文件路径
            ext: ".css", //输出的文件扩展名
          },
        ],
      },
    },
    //转换js
    babel: {
      options: {
        sourceMap: true,
        presets: ["@babel/preset-env"],
      },
      dist: {
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["**/*.js"],
            dest: "temp",
            ext: ".js",
          },
        ],
      },
    },

    //图片复制到

    //public 复制到dist

    //合并引用文件 在编译之后合并
    useref: {
      html: "temp/*.html",
    },
    //压缩文件css
    cssmin: {
      dist: {
        files: [
          {
            expand: true,
            cwd: "temp",
            src: ["**/*.css", "!*.min.css"],
            dest: "dist",
            ext: ".min.css",
          },
        ],
      },
    },
    //压缩html
    htmlmin: {
      dist: {
        options: {
          removeComments: true,
          collapseWhitespace: true,
          minifyCSS: true, //行内css压缩
          minifyJS: true, //行内js压缩
        },
        files: [
          {
            expand: true,
            cwd: "temp",
            src: "*.html",
            dest: "dist",
          },
        ],
      },
    },
    //压缩js
    uglify: {
      dist: {
        files: [
          {
            expand: true,
            cwd: ".temp",
            src: "**/*.js",
            dest: "dist",
            ext: ".min.js",
          },
        ],
      },
    },
    //开发服务器，打开浏览器，热更新到浏览器
    browserSync: {
      bsFiles: {
        src: "temp",
      },
      options: {
        notify: false,
        watchTask: true,
        server: {
          baseDir: "temp/",
          routes: {
            "/node_modules": "node_modules",
          },
        },
        browser: ["google chrome", "Safair"],
        notify: false,
      },
    },
    //监听源文件变化，编译复制到temp
    watch: {
      js: {
        files: ["src/assets/scripts/*.js"],
        tasks: ["babel"],
      },
      css: {
        files: ["src/assets/styles/*.scss"],
        tasks: ["sass"],
      },
      html: {
        files: ["src/*.html"],
        tasks: ["web_swig"],
      },
    },
  });
  //加载任务
  loadGruntTasks(grunt);
  //配置列表里的任务都可以通过grunt XXX 执行，不需要在grunt.registerTask注册也可以
  //任务别名和任务列表不可以重名

  //编译
  grunt.registerTask("compile", ["clean", "sass", "babel", "web_swig"]);
  //压缩   我的想法是开发阶段不压缩，上线阶段再进行压缩
  grunt.registerTask("min", ["cssmin", "htmlmin", "uglify"]);

  //开发环境
  grunt.registerTask("develop", ["compile", "browserSync", "watch"]);
  //生产环境，清空，编译，处理引用文件，压缩 ，构建
  grunt.registerTask("build", ["compile", "useref", "min"]);
};
